package com.moba.service;

import com.moba.domain.UserBiRecord;
import com.moba.domain.UserInfo;
import com.moba.emums.BiGetType;
import com.moba.emums.BiType;
import com.moba.mapper.UserBiRecordMapper;
import com.moba.mapper.UserInfoMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @author ding, ning
 * @version 2019-03-06 11:07
 * @date 2019-03-06 11:07
 */
@Service
@Transactional
public class UserBiRecordService {

  @Resource private UserBiRecordMapper userBiRecordMapper;

  @Resource private UserInfoMapper userInfoMapper;

  public UserBiRecord getEntity(UserBiRecord t) {
    return this.userBiRecordMapper.selectEntity(t);
  }

  public Integer getEntityCount(UserBiRecord t) {
    return this.userBiRecordMapper.selectEntityCount(t);
  }

  public BigDecimal getEntityBiSum(UserBiRecord t) {
    return this.userBiRecordMapper.selectEntityBiSum(t);
  }

  public List<UserBiRecord> getEntityList(UserBiRecord t) {
    return this.userBiRecordMapper.selectEntityList(t);
  }

  public Integer createEntity(UserBiRecord t) {
    this.userBiRecordMapper.insertEntity(t);
    return t.getId();
  }

  public int updateEntity(UserBiRecord t) {
    return this.userBiRecordMapper.updateEntity(t);
  }

  public int removeEntity(UserBiRecord t) {
    return this.userBiRecordMapper.deleteEntity(t);
  }

  /**
   * 增加数据记录
   *
   * @param addUserId
   * @param biGetType
   * @param biChuOrRu
   * @param biNo
   * @return
   */
  public Boolean addBiRecord(
      Integer addUserId,
      Integer linkId,
      Integer biGetType,
      Integer biType,
      Integer biChuOrRu,
      BigDecimal biNo,
      Integer orderId) {

    Boolean isSuccess = false;

    UserInfo userInfo = new UserInfo();
    userInfo.setId(addUserId);
    userInfo = userInfoMapper.selectEntity(userInfo);

    UserBiRecord userBiRecord = new UserBiRecord();
    userBiRecord.setAddUname(userInfo.getUserName());
    userBiRecord.setAddUserId(addUserId);
    userBiRecord.setBiGetType(biGetType);
    userBiRecord.setBiNo(biNo);
    userBiRecord.setBiType(biType);
    userBiRecord.setAddDate(new Date());
    if (null != orderId) {
      userBiRecord.setOrderId(orderId);
    }
    userBiRecord.setBiGetMemo(BiGetType.getName(biGetType));
    userBiRecord.setBiChuOrRu(biChuOrRu);
    if (linkId != null) {
      userBiRecord.setLinkId(linkId);
    }

    if (biType.intValue() == BiType.BI_TYPE_100.getIndex()) {
      userBiRecord.setBiNoBefore(userInfo.getBiDianzi());
      if (biChuOrRu > 0) {
        userBiRecord.setBiNoAfter(userInfo.getBiDianzi().add(biNo));
      } else {
        userBiRecord.setBiNoAfter(userInfo.getBiDianzi().subtract(biNo));
      }
    }

    if (biType.intValue() == BiType.BI_TYPE_200.getIndex()) {
      userBiRecord.setBiNoBefore(userInfo.getLeijiMoneyUser());
      if (biChuOrRu > 0) {
        userBiRecord.setBiNoAfter(userInfo.getLeijiMoneyUser().add(biNo));
      } else {
        userBiRecord.setBiNoAfter(userInfo.getLeijiMoneyUser().subtract(biNo));
      }
    }
    if (biType.intValue() == BiType.BI_TYPE_300.getIndex()) {
      userBiRecord.setBiNoBefore(userInfo.getBiUsdt());
      if (biChuOrRu > 0) {
        userBiRecord.setBiNoAfter(userInfo.getBiUsdt().add(biNo));
      } else {
        userBiRecord.setBiNoAfter(userInfo.getBiUsdt().subtract(biNo));
      }
    }

    if (biType.intValue() == BiType.BI_TYPE_400.getIndex()) {
      userBiRecord.setBiNoBefore(userInfo.getOrderMoneySum());
      if (biChuOrRu > 0) {
        userBiRecord.setBiNoAfter(userInfo.getOrderMoneySum().add(biNo));
      } else {
        userBiRecord.setBiNoAfter(userInfo.getOrderMoneySum().subtract(biNo));
      }
    }

    if (biType.intValue() == BiType.BI_TYPE_500.getIndex()) {
      userBiRecord.setBiNoBefore(userInfo.getGusd());
      if (biChuOrRu > 0) {
        userBiRecord.setBiNoAfter(userInfo.getGusd().add(biNo));
      } else {
        userBiRecord.setBiNoAfter(userInfo.getGusd().subtract(biNo));
      }
    }

    userBiRecordMapper.insertEntity(userBiRecord);
    if (userBiRecord.getId() > 0) {
      isSuccess = true;
    }
    return isSuccess;
  }

  public List<UserBiRecord> getEntityPaginatedList(UserBiRecord t) {
    return this.userBiRecordMapper.selectEntityPaginatedList(t);
  }
}
